﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace KiraSh.ResumeTracker
{
    /// <summary>
    /// Profile manager loads/write profiles into profile.xml file.
    /// Stores all the profiles current on the computer.
    /// </summary>
    public static class ProfileManager
    {
        public static string profilePath = PathHelper.MapCombine("profiles.xml");

        public const string POSTINGS_DIR = "postings";
        public const string RESUMES_DIR = "resumes";
        public const string COMPANIES_DIR = "companies";

        private static List<Profile> profiles;

        public static void LoadFromDisk()
        {
            LoadFromDisk(profilePath);
        }

        public static void LoadFromDisk(string path)
        {
            XMLDealer xml = new XMLDealer();

            if (File.Exists(path))
            {

                profiles = (List<Profile>)xml.LoadXML(path, typeof(List<Profile>));
                if (profiles == null)
                {
                    throw new Exception("Profiles file invalid. " + path);
                }
            }
            else
            {
                profiles = new List<Profile>();
            }
        }

        public static void SaveToDisk()
        {
            SaveToDisk(profilePath);
        }

        public static void SaveToDisk(string path)
        {
            XMLDealer xml = new XMLDealer();

            if (profiles == null)
            {
                throw new Exception("Profiles are null. Please load from disk first.");
            }

            xml.WriteXML(path, typeof(List<Profile>), profiles);

            // create profile structure for each profile
            foreach (Profile profile in profiles)
            {
                string profileDir = PathHelper.MapCombine(profile.Name);
                Directory.CreateDirectory(profileDir);
                Directory.CreateDirectory(PathHelper.Combine(profileDir, COMPANIES_DIR));
                Directory.CreateDirectory(PathHelper.Combine(profileDir, POSTINGS_DIR));
                Directory.CreateDirectory(PathHelper.Combine(profileDir, RESUMES_DIR));
            }

        }

        public static List<Profile> Profiles
        {
            get
            {
                return profiles;
            }
        }

        public static Profile CreateNewProfile(string name)
        {
            // create temp profile and get the selection from the drop box
            Profile newProfile = new Profile();

            // create new profile object
            if ( name.Trim().Length > 0)
            {
                newProfile.Name = name;
                // make sure the profile doesn't already exist
                if (profiles.Contains(newProfile))
                {
                    throw new Exception("Profile "+name+" already exists.");
                }
                else
                {
                    newProfile.Path = PathHelper.MapCombine(newProfile.Name, "MasterResume.doc");
                    // add profiles to the list and xml file
                    Profiles.Add(newProfile);
                    return newProfile;
                }
            }
            else
            {
                throw new Exception("Profile name cannot be empty.");
            }
        }
    }
}